package com.sk.weichat.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.weichat.R;
import com.sk.weichat.bean.message.GroupMessage;
import com.sk.weichat.db.dao.GroupMessageDao;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.ui.contacts.GroupMessageActivity;
import com.sk.weichat.util.ToastUtil;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.Call;

/**
 * 群通知列表适配器
 * create by swy 2019-09-20
 */
public class GroupMessageAdapter extends BaseMultiItemQuickAdapter<MultipleGroupMessageItem, BaseViewHolder> {
    private Context mContext;
    private ImageView avatar_img;
    private TextView nick_name_tv;
    private TextView tvContent;
    private TextView tvOpinion;
    private TextView tvHandler;
    private TextView tvStatus;

    public GroupMessageAdapter(Activity context, List<MultipleGroupMessageItem> itemList) {
        super(itemList);
        mContext = context;
        addItemType(MultipleGroupMessageItem.ITEM_ROW_THREE, R.layout.row_group_message);
        addItemType(MultipleGroupMessageItem.ITEM_ROW_FOUR, R.layout.row_group_message_four);
    }

    @Override
    protected void convert(BaseViewHolder holder, MultipleGroupMessageItem item) {
        avatar_img = holder.getView(R.id.avatar_img);
        nick_name_tv = holder.getView(R.id.nick_name_tv);
        tvContent = holder.getView(R.id.tv_content);
        tvOpinion = holder.getView(R.id.tv_opinion);
        tvHandler = holder.getView(R.id.tv_handler);
        tvStatus = holder.getView(R.id.tv_status);
        tvStatus.setClickable(false);
        holder.addOnClickListener(R.id.delete_tv);
        holder.addOnClickListener(R.id.layout_group_message_item);
        holder.addOnClickListener(R.id.avatar_img);
        fillGroupMessageState(holder, item.getData());
    }

    private void fillGroupMessageState(BaseViewHolder holder, GroupMessage groupMessage) {
        switch (groupMessage.getState()) {
            case GroupMessage.STATUS_GM_10:
                //设置管理员
                setGroup10(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_11:
                //取消管理员
                setGroup11(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_12:
                //你退出群
                setGroup12(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_13:
                //你被移出群
                setGroup13(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_14:
                //其他人退出群
                setGroup14(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_15:
                //其他人被踢出群
                setGroup15(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_16:
                //邀请进群
                setGroup16(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_17:
                //申请进群
                setGroup17(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_18:
                //转让群主给你
                setGroup18(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_19:
                //转让群主给你他人
                setGroup19(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_20:
                //你的邀请被拒绝
                setGroup20(holder, groupMessage);
                break;
            case GroupMessage.STATUS_GM_21:
                //你的申请被拒绝
                setGroup21(holder, groupMessage);
                break;
        }

    }

    private void setGroup10(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getRoomName());
        String content = "";
        if (groupMessage.getOwnerId().equals(groupMessage.getToUserId())) {
            content = "已将你设置为管理员";
        } else {
            content = "将" + groupMessage.getToUserName() + "设置为管理员";
        }
        AvatarHelper.getInstance().displayAvatar(groupMessage.getOwnerId(),groupMessage, avatar_img);
        tvContent.setText(content);
        tvHandler.setVisibility(View.VISIBLE);
        tvHandler.setText("处理人：" + groupMessage.getHandler());
        tvOpinion.setVisibility(View.GONE);
        tvStatus.setVisibility(View.INVISIBLE);
    }

    private void setGroup11(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getRoomName());
        String content = "";
        if (groupMessage.getOwnerId().equals(groupMessage.getToUserId())) {
            content = "已取消你的管理员身份";
        } else {
            content = "已取消 " + groupMessage.getToUserName() + "的管理员身份";
        }
        AvatarHelper.getInstance().displayAvatar(groupMessage.getOwnerId(),groupMessage, avatar_img);
        tvContent.setText(content);
        tvHandler.setVisibility(View.VISIBLE);
        tvHandler.setText("处理人：" + groupMessage.getHandler());
        tvOpinion.setVisibility(View.GONE);
        tvStatus.setVisibility(View.INVISIBLE);
    }

    private void setGroup12(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getRoomName());
        String content = "你退出 " + groupMessage.getRoomName();
        AvatarHelper.getInstance().displayAvatar(groupMessage.getOwnerId(),groupMessage, avatar_img);
        tvContent.setText(content);
        tvHandler.setVisibility(View.GONE);
        tvOpinion.setVisibility(View.GONE);
        tvStatus.setVisibility(View.INVISIBLE);
    }

    private void setGroup13(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getRoomName());
        String content = "已将你移出群";
        tvContent.setText(content);
        AvatarHelper.getInstance().displayAvatar(groupMessage.getOwnerId(),groupMessage, avatar_img);
        tvHandler.setVisibility(View.VISIBLE);
        tvHandler.setText("处理人：" + groupMessage.getHandler());
        tvOpinion.setVisibility(View.GONE);
        tvStatus.setVisibility(View.INVISIBLE);
    }

    private void setGroup14(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getToUserName());
        String content = "退出群 " + groupMessage.getRoomName();
        tvContent.setText(content);
        AvatarHelper.getInstance().displayAvatar(groupMessage.getToUserId(), avatar_img, true);
        tvHandler.setVisibility(View.GONE);
        tvOpinion.setVisibility(View.GONE);
        tvStatus.setVisibility(View.INVISIBLE);
    }

    private void setGroup15(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getRoomName());
        String content = "已将 " + groupMessage.getToUserName() + "移出群";
        tvContent.setText(content);
        AvatarHelper.getInstance().displayAvatar(groupMessage.getOwnerId(),groupMessage, avatar_img);
        tvHandler.setVisibility(View.VISIBLE);
        tvHandler.setText("处理人：" + groupMessage.getHandler());
        tvOpinion.setVisibility(View.GONE);
        tvStatus.setVisibility(View.INVISIBLE);
    }

    private void setGroup16(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getFromUserName());
        String[] ids = groupMessage.getToUserId().split(",");
        String content = "邀请 " + ids.length + "位好友加入群 "+groupMessage.getRoomName();
        tvContent.setText(content);
        AvatarHelper.getInstance().displayAvatar(groupMessage.getFromUserId(), avatar_img, true);
        handlerGroupMsg(groupMessage);

    }

    private void setGroup17(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getFromUserName());
        String content = "申请加入群 "+groupMessage.getRoomName();
        tvContent.setText(content);
        AvatarHelper.getInstance().displayAvatar(groupMessage.getFromUserId(), avatar_img, true);
        handlerGroupMsg(groupMessage);

    }

    private void setGroup18(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getRoomName());
        String content = "将群转让给你";
        tvContent.setText(content);
        AvatarHelper.getInstance().displayAvatar(groupMessage.getOwnerId(),groupMessage, avatar_img);
        tvHandler.setVisibility(View.VISIBLE);
        tvHandler.setText("处理人：" + groupMessage.getHandler());
        tvOpinion.setVisibility(View.GONE);
        tvStatus.setVisibility(View.INVISIBLE);
    }

    private void setGroup19(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getRoomName());
        String content = "将群转让给" + groupMessage.getToUserName();
        tvContent.setText(content);
        AvatarHelper.getInstance().displayAvatar(groupMessage.getOwnerId(),groupMessage, avatar_img);
        tvHandler.setVisibility(View.VISIBLE);
        tvHandler.setText("处理人：" + groupMessage.getHandler());
        tvOpinion.setVisibility(View.GONE);
        tvStatus.setVisibility(View.INVISIBLE);
    }

    private void setGroup20(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getRoomName());
        String content = "拒绝你的群邀请";
        tvContent.setText(content);
        AvatarHelper.getInstance().displayAvatar(groupMessage.getOwnerId(),groupMessage, avatar_img);

        if (TextUtils.isEmpty(groupMessage.getReason())) {
            tvOpinion.setVisibility(View.GONE);
        } else {
            tvOpinion.setVisibility(View.VISIBLE);
            tvOpinion.setText(groupMessage.getOpinion());
        }

        if (TextUtils.isEmpty(groupMessage.getHandler())) {
            tvHandler.setVisibility(View.GONE);
        } else {
            tvHandler.setVisibility(View.VISIBLE);
            tvHandler.setText("处理人：" + groupMessage.getHandler());
        }
        tvStatus.setVisibility(View.INVISIBLE);
    }

    private void setGroup21(BaseViewHolder holder, GroupMessage groupMessage) {
        nick_name_tv.setText(groupMessage.getRoomName());
        String content = "已拒绝让你加入群";
        tvContent.setText(content);
        AvatarHelper.getInstance().displayAvatar(groupMessage.getOwnerId(),groupMessage, avatar_img);

        if (TextUtils.isEmpty(groupMessage.getReason())) {
            tvOpinion.setVisibility(View.GONE);
        } else {
            tvOpinion.setVisibility(View.VISIBLE);
            tvOpinion.setText(groupMessage.getOpinion());
        }

        if (TextUtils.isEmpty(groupMessage.getHandler())) {
            tvHandler.setVisibility(View.GONE);
        } else {
            tvHandler.setVisibility(View.VISIBLE);
            tvHandler.setText("处理人：" + groupMessage.getHandler());
        }
        tvStatus.setVisibility(View.INVISIBLE);
    }

    private void handlerGroupMsg(GroupMessage groupMessage) {
        if (TextUtils.isEmpty(groupMessage.getReason())) {
            tvOpinion.setVisibility(View.GONE);
        } else {
            tvOpinion.setVisibility(View.VISIBLE);
            tvOpinion.setText(groupMessage.getReason());
        }

        if (TextUtils.isEmpty(groupMessage.getHandler())) {
            tvHandler.setVisibility(View.GONE);
        } else {
            tvHandler.setVisibility(View.VISIBLE);
            tvHandler.setText("处理人：" + groupMessage.getHandler());
        }

        showStateBtn(groupMessage);
    }

    private void showStateBtn(GroupMessage groupMessage) {
        tvStatus.setVisibility(View.VISIBLE);
        switch (groupMessage.getMsgState()) {
            //0 未处理 1 同意 2 拒绝 3过期 4已加入 5 失效 6超员
            case 0://未处理
                tvStatus.setText("同意");
                tvStatus.setClickable(true);
                tvStatus.setBackgroundResource(R.drawable.bg_gray_radius26dp);
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.text_color_tip));
                tvStatus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        agreeInvite(groupMessage);
                    }
                });
                break;
            case 1://已同意
                tvStatus.setText("已同意");
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.text_color_content));

                tvStatus.setBackgroundResource(R.drawable.bg_trans_radius26dp);
                break;
            case 2://已拒绝
                tvStatus.setText("已拒绝");
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.text_color_content));

                tvStatus.setBackgroundResource(R.drawable.bg_trans_radius26dp);
                break;
            case 3://已过期
                tvStatus.setText("已过期");
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.text_color_content));

                tvStatus.setBackgroundResource(R.drawable.bg_trans_radius26dp);
                break;
            case 4://已加入
                tvStatus.setText("已加入");
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.text_color_content));

                tvStatus.setBackgroundResource(R.drawable.bg_trans_radius26dp);
                break;
            case 5://已失效
                tvStatus.setText("已失效");
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.text_color_content));

                tvStatus.setBackgroundResource(R.drawable.bg_trans_radius26dp);
                break;
            default:
                tvStatus.setBackgroundResource(R.drawable.bg_trans_radius26dp);
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.text_color_content));

        }
    }

    /**
     * @param groupMessage
     * @author: czl
     * @description 同意邀请进群
     * @date: 2019/9/18 20:54
     */
    private void agreeInvite(GroupMessage groupMessage) {
        DialogHelper.showDefaulteMessageProgressDialog(mContext);

        Map<String, String> params = new HashMap<>();
        params.put("access_token", ((GroupMessageActivity) mContext).coreManager.getSelfStatus().accessToken);
        params.put("msgId", groupMessage.getPacketId());
        HttpUtils.post().url(((GroupMessageActivity) mContext).coreManager.getConfig().ROOM_JOIN_AGREE)
                .params(params).build().execute(new BaseCallback<String>(String.class) {

            @Override
            public void onResponse(ObjectResult<String> result) {
                DialogHelper.dismissProgressDialog();
                if (result.getResultCode() == 1) {//同意邀请进群
                    DialogHelper.dismissProgressDialog();
                    GroupMessageDao.getInstance().updateMsgState(((GroupMessageActivity) mContext).coreManager.getSelf().getUserId(), groupMessage.getPacketId(), 1);//0 未处理 1 同意 2 拒绝 3过期 4已加入 5 失效 6超员
                    groupMessage.setMsgState(1);
                    notifyDataSetChanged();
                } else if (result.getResultCode() == 1040321) {//已过期
                    GroupMessageDao.getInstance().updateMsgState(((GroupMessageActivity) mContext).coreManager.getSelf().getUserId(), groupMessage.getPacketId(), 3);//0 未处理 1 同意 2 拒绝 3过期 4已加入 5 失效 6超员
                    groupMessage.setMsgState(3);
                    notifyDataSetChanged();
                } else if (result.getResultCode() == 1040322) {//已失效
                    GroupMessageDao.getInstance().updateMsgState(((GroupMessageActivity) mContext).coreManager.getSelf().getUserId(), groupMessage.getPacketId(), 5);//0 未处理 1 同意 2 拒绝 3过期 4已加入 5 失效 6超员
                    groupMessage.setMsgState(5);
                    notifyDataSetChanged();
                } else {
                    ToastUtil.showToast(mContext, result.getResultMsg());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                DialogHelper.dismissProgressDialog();
                ToastUtil.showErrorNet(mContext);
            }
        });
    }

}
